<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%region}}`.
 */
class m231025_063829_create_region_table extends Migration
{
    const TABLE_NAME = '{{%region}}';

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=MyISAM COMMENT="地区表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey()->comment('ID'),
            'name' => $this->string(50)->notNull()->comment('名称'),
            'pid' => $this->integer()->notNull()->comment('父ID'),
            'level' => $this->tinyInteger()->notNull()->comment('级联'),
            'letter' => $this->char(1)->notNull()->comment('首字母'),
            'sort' => $this->integer()->notNull()->defaultValue(255)->comment('排序'),
            'status' => $this->boolean()->notNull()->defaultValue(1)->comment('状态,0:禁用 1:正常'),
        ], $tableOptions);

        $this->createIndex('idx_pid', self::TABLE_NAME, 'pid');
        $this->createIndex('idx_name', self::TABLE_NAME, 'name');
        $this->createIndex('idx_letter', self::TABLE_NAME, 'letter');

        $this->createIndex('idx_level', self::TABLE_NAME, 'level');

        $this->db->createCommand(file_get_contents(__DIR__ . '/sql/region.sql'))->execute();
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
